const readline = require('readline'); // 用于读取用户输入
const express = require('express');
const mysql = require('mysql2');
const bcrypt = require('bcrypt');
const app = express();
const port = 3000;

// 设置密码
const correctPassword = 'your_password_here'; // 将 'your_password_here' 替换为你想要设置的密码

// 创建读取接口
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// 提示用户输入密码
rl.question('请输入密码以继续使用该应用程序: ', (input) => {
    if (input === correctPassword) {
        console.log('密码正确，正在启动应用程序...');
        rl.close(); // 关闭读取接口

        // 应用程序逻辑开始
        // 创建MySQL连接池
        const pool = mysql.createPool({
            host: 'localhost',
            user: 'root',
            password: 'm4a1123',
            database: 'my_database'
        });

        // 删除用户信息的接口
        app.delete('/account/delete/:id', (req, res) => {
            const userId = req.params.id; // 获取请求中的用户ID

            const deleteQuery = 'DELETE FROM account WHERE id = ?';
            pool.query(deleteQuery, [userId], (error, results) => {
                if (error) {
                    res.status(500).json({ code: "500", msg: "数据库删除失败", error });
                    return;
                }

                if (results.affectedRows > 0) {
                    res.json({ code: "200", msg: "用户删除成功" });
                } else {
                    res.json({ code: "404", msg: "未找到用户，删除失败" });
                }
            });
        });

        // 用户登录的接口
        app.get('/account/login', (req, res) => {
            const username = req.query.username;
            const password = req.query.password;

            const selectQuery = 'SELECT * FROM account WHERE username = ? AND password = ?';
            pool.query(selectQuery, [username, password], (error, results) => {
                if (error) {
                    res.status(500).json({ code: "500", msg: "数据库查询失败", error });
                    return;
                }

                if (results.length > 0) {
                    res.json({ code: "200", msg: "登录成功" });
                } else {
                    res.json({ code: "404", msg: "用户名或密码错误" });
                }
            });
        });

        // 处理GET请求，查询用户信息
        app.get('/account/info', (req, res) => {
            const userId = 1; // 假设查询ID为1的用户

            pool.query('SELECT * FROM account WHERE id = ?', [userId], (error, results) => {
                if (error) {
                    res.status(500).json({ code: "500", msg: "数据库查询失败", error });
                    return;
                }

                if (results.length > 0) {
                    res.json({
                        code: "200",
                        msg: "操作成功",
                        data: results[0]
                    });
                } else {
                    res.json({
                        code: "404",
                        msg: "未找到用户信息"
                    });
                }
            });
        });

        // 启动服务器
        app.listen(port, () => {
            console.log(`Server is running at http://localhost:${port}`);
        });

    } else {
        console.log('密码错误，无法启动应用程序。');
        rl.close(); // 关闭读取接口
        process.exit(1); // 退出程序
    }
});
